package com.nytimes.ohos.store.sample.data.model;

import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.List;

/**
 * Immutable implementation of {@link Image}.
 * <p>
 * Use the builder to create immutable instances:
 * {@code ImmutableImage.builder()}.
 */
@SuppressWarnings("all")
public final class ImmutableImage extends Image {
  private final String url;
  private final int height;
  private final int width;

  private ImmutableImage(String url, int height, int width) {
    this.url = url;
    this.height = height;
    this.width = width;
  }

  /**
   * url
   *
   * @return The value of the {@code url} attribute
   */
  @Override
  public String url() {
    return url;
  }

  /**
   * height
   *
   * @return The value of the {@code height} attribute
   */
  @Override
  public int height() {
    return height;
  }

  /**
   * width
   *
   * @return The value of the {@code width} attribute
   */
  @Override
  public int width() {
    return width;
  }

  /**
   * Copy the current immutable object by setting a value for the {@link Image#url() url} attribute.
   * An equals check used to prevent copying of the same value by returning {@code this}.
   *
   * @param url A new value for url
   * @return A modified copy of the {@code this} object
   */
  public final ImmutableImage withUrl(String url) {
    if (this.url.equals(url)) return this;
    String newValue = Preconditions.checkNotNull(url, "url");
    return new ImmutableImage(newValue, this.height, this.width);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link Image#height() height} attribute.
   * A value equality check is used to prevent copying of the same value by returning {@code this}.
   *
   * @param height A new value for height
   * @return A modified copy of the {@code this} object
   */
  public final ImmutableImage withHeight(int height) {
    if (this.height == height) return this;
    return new ImmutableImage(this.url, height, this.width);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link Image#width() width} attribute.
   * A value equality check is used to prevent copying of the same value by returning {@code this}.
   *
   * @param width A new value for width
   * @return A modified copy of the {@code this} object
   */
  public final ImmutableImage withWidth(int width) {
    if (this.width == width) return this;
    return new ImmutableImage(this.url, this.height, width);
  }

  /**
   * This instance is equal to all instances of {@code ImmutableImage} that have equal attribute values.
   *
   * @param another Object
   * @return {@code true} if {@code this} is equal to {@code another} instance
   */
  @Override
  public boolean equals(Object another) {
    if (this == another) return true;
    return another instanceof ImmutableImage
        && equalTo((ImmutableImage) another);
  }

  private boolean equalTo(ImmutableImage another) {
    return url.equals(another.url)
        && height == another.height
        && width == another.width;
  }

  /**
   * Computes a hash code from attributes: {@code url}, {@code height}, {@code width}.
   *
   * @return hashCode value
   */
  @Override
  public int hashCode() {
    int h = 31;
    h = h * 17 + url.hashCode();
    h = h * 17 + height;
    h = h * 17 + width;
    return h;
  }

  /**
   * Prints the immutable value {@code Image} with attribute values.
   *
   * @return A string representation of the value
   */
  @Override
  public String toString() {
    return MoreObjects.toStringHelper("Image")
        .omitNullValues()
        .add("url", url)
        .add("height", height)
        .add("width", width)
        .toString();
  }

  /**
   * Creates an immutable copy of a {@link Image} value.
   * Uses accessors to get values to initialize the new immutable instance.
   * If an instance is already immutable, it is returned as is.
   *
   * @param instance The instance to copy
   * @return A copied immutable Image instance
   */
  public static ImmutableImage copyOf(Image instance) {
    if (instance instanceof ImmutableImage) {
      return (ImmutableImage) instance;
    }
    return ImmutableImage.builder()
        .from(instance)
        .build();
  }

  /**
   * Creates a builder for {@link ImmutableImage ImmutableImage}.
   *
   * @return A new ImmutableImage builder
   */
  public static Builder builder() {
    return new Builder();
  }

  /**
   * Builds instances of type {@link ImmutableImage ImmutableImage}.
   * Initialize attributes and then invoke the {@link #build()} method to create an
   * immutable instance.
   * <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or collection,
   * but instead used immediately to create instances.</em>
   */
  public static final class Builder {
    private static final long INIT_BIT_URL = 0x1L;
    private static final long INIT_BIT_HEIGHT = 0x2L;
    private static final long INIT_BIT_WIDTH = 0x4L;
    private long initBits = 0x7L;

    private String url;
    private int height;
    private int width;

    private Builder() {
    }

    /**
     * Fill a builder with attribute values from the provided {@code Image} instance.
     * Regular attribute values will be replaced with those from the given instance.
     * Absent optional values will not replace present values.
     *
     * @param instance The instance from which to copy values
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder from(Image instance) {
      Preconditions.checkNotNull(instance, "instance");
      url(instance.url());
      height(instance.height());
      width(instance.width());
      return this;
    }

    /**
     * Initializes the value for the {@link Image#url() url} attribute.
     *
     * @param url The value for url 
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder url(String url) {
      this.url = Preconditions.checkNotNull(url, "url");
      initBits &= ~INIT_BIT_URL;
      return this;
    }

    /**
     * Initializes the value for the {@link Image#height() height} attribute.
     *
     * @param height The value for height 
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder height(int height) {
      this.height = height;
      initBits &= ~INIT_BIT_HEIGHT;
      return this;
    }

    /**
     * Initializes the value for the {@link Image#width() width} attribute.
     *
     * @param width The value for width 
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder width(int width) {
      this.width = width;
      initBits &= ~INIT_BIT_WIDTH;
      return this;
    }

    /**
     * Builds a new {@link ImmutableImage ImmutableImage}.
     *
     * @return An immutable instance of Image
     * @throws IllegalStateException if any required attributes are missing
     */
    public ImmutableImage build() {
      if (initBits != 0) {
        throw new IllegalStateException(formatRequiredAttributesMessage());
      }
      return new ImmutableImage(url, height, width);
    }

    private String formatRequiredAttributesMessage() {
      List<String> attributes = Lists.newArrayList();
      if ((initBits & INIT_BIT_URL) != 0) attributes.add("url");
      if ((initBits & INIT_BIT_HEIGHT) != 0) attributes.add("height");
      if ((initBits & INIT_BIT_WIDTH) != 0) attributes.add("width");
      return "Cannot build Image, some of required attributes are not set " + attributes;
    }
  }
}
